<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

class LoginController extends Controller{
	public function index(){
		if(IS_POST){
			
			$code = Q('post.code','','strtoupper');
			$username = Q('post.username');
			$password = Q('post.password');
//			print_r($username);exit;
			if($code!=$_SESSION['code']) View::error('验证码有误');
			//$user = Db::select("SELECT uid,username,password FROM user WHERE username = ?",["$username"]);
			$user = Db::table('admin')->where("username='{$username}'")->get();
			//p($user);
			if(empty($user)) View::error('用户名或密码错误');
			//p($user);exit;
			if($user[0]['password']!=md5($password)) View::error('用户或密码错误');
			
			//下面开始登陆成功
			$_SESSION['aid']=$user[0]['aid'];
			go(U('Index/index'));exit;
			
		}				
		//载入登录页面	
		View::make();
	}
	
	//退出登录
	public function out(){
		session_unset();
		session_destroy();
		header('Location: '.U('Login/index'));
	}
	
	public function code(){
//		$data = ob_get_contents();
//		ob_clean();
//		var_dump($data);exit;
		ob_clean();
		Code::num(1)->width(120)->height(44)->make();
	}
	
	//控制父窗口跳转
	public function parentHref(){
		echo '<script>window.parent.location.href="'.U('Login/index').'"</script>';
	}
	
}



    
?>